<?php

namespace App\Console\Commands\Test;

use Illuminate\Console\Command;
use App\Http\Controllers\Admin\AdminRechargeController;
use App\Http\Controllers\Admin\AdminBankDetailController;
use App\Services\Bank\BankServiceManager;
use App\Models\Recharge;
use App\Models\BankDetailHangzhou;
use Illuminate\Http\Request;

class TestAdminControllers extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'admin:test-controllers
                            {--test-recharge : 测试充值控制器}
                            {--test-bank-detail : 测试银行明细控制器}
                            {--test-all : 测试所有控制器}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '测试Admin控制器基本功能';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->info('=== Admin控制器功能测试 ===');

        if ($this->option('test-recharge') || $this->option('test-all')) {
            $this->testRechargeController();
        }

        if ($this->option('test-bank-detail') || $this->option('test-all')) {
            $this->testBankDetailController();
        }

        if (!$this->option('test-recharge') &&
            !$this->option('test-bank-detail') &&
            !$this->option('test-all')) {
            $this->showHelp();
        }

        return 0;
    }

    /**
     * 测试充值控制器
     */
    private function testRechargeController()
    {
        $this->info('测试AdminRechargeController...');

        try {
            $bankService = app(BankServiceManager::class);
            $controller = new AdminRechargeController($bankService);

            // 测试获取充值列表
            $request = new Request();
            $request->merge([
                'per_page' => 5
            ]);

            $response = $controller->index($request);
            $data = $response->getData(true);

            if ($data['code'] === 0) {
                $this->line("✓ 充值列表接口正常");
                $this->line("  - 总记录数: " . ($data['data']['pagination']['total'] ?? 0));
                $this->line("  - 状态统计: " . json_encode($data['data']['statistics']['status_counts'] ?? []));
            } else {
                $this->error("✗ 充值列表接口失败: " . $data['message']);
            }

            // 测试获取充值详情（如果有数据）
            $recharge = Recharge::first();
            if ($recharge) {
                $response = $controller->show($recharge->id);
                $data = $response->getData(true);

                if ($data['code'] === 0) {
                    $this->line("✓ 充值详情接口正常");
                    $this->line("  - 充值单号: " . ($data['data']['recharge']['recharge_number'] ?? 'N/A'));
                    $this->line("  - 匹配建议数: " . count($data['data']['match_suggestions'] ?? []));
                } else {
                    $this->error("✗ 充值详情接口失败: " . $data['message']);
                }
            } else {
                $this->line("! 没有充值数据，跳过详情测试");
            }

            // 测试手动执行匹配算法
            $request = new Request();
            $response = $controller->runMatching($request);
            $data = $response->getData(true);

            if ($data['code'] === 0) {
                $this->line("✓ 匹配算法接口正常");
                $this->line("  - 总充值数: " . ($data['data']['total_recharges'] ?? 0));
                $this->line("  - 匹配成功数: " . ($data['data']['matched_count'] ?? 0));
                $this->line("  - 成功率: " . ($data['data']['success_rate'] ?? 0) . "%");
            } else {
                $this->error("✗ 匹配算法接口失败: " . $data['message']);
            }

        } catch (\Exception $e) {
            $this->error("AdminRechargeController测试失败: " . $e->getMessage());
        }
    }

    /**
     * 测试银行明细控制器
     */
    private function testBankDetailController()
    {
        $this->info('测试AdminBankDetailController...');

        try {
            $controller = new AdminBankDetailController();

            // 测试获取银行明细列表
            $request = new Request();
            $request->merge([
                'per_page' => 5
            ]);

            $response = $controller->index($request);
            $data = $response->getData(true);

            if ($data['code'] === 0) {
                $this->line("✓ 银行明细列表接口正常");
                $this->line("  - 总记录数: " . ($data['data']['pagination']['total'] ?? 0));
                $this->line("  - 匹配状态统计: " . json_encode($data['data']['statistics']['match_status_counts'] ?? []));
            } else {
                $this->error("✗ 银行明细列表接口失败: " . $data['message']);
            }

            // 测试获取银行明细详情（如果有数据）
            $detail = BankDetailHangzhou::first();
            if ($detail) {
                $response = $controller->show($detail->id);
                $data = $response->getData(true);

                if ($data['code'] === 0) {
                    $this->line("✓ 银行明细详情接口正常");
                    $this->line("  - 流水号: " . ($data['data']['detail']['serial_no'] ?? 'N/A'));
                    $this->line("  - 匹配建议数: " . count($data['data']['match_suggestions'] ?? []));
                } else {
                    $this->error("✗ 银行明细详情接口失败: " . $data['message']);
                }

                // 测试获取匹配建议
                if ($detail->match_status === 'unmatched') {
                    $response = $controller->matchSuggestions($detail->id);
                    $data = $response->getData(true);

                    if ($data['code'] === 0) {
                        $this->line("✓ 匹配建议接口正常");
                        $this->line("  - 建议数量: " . count($data['data'] ?? []));
                    } else {
                        $this->error("✗ 匹配建议接口失败: " . $data['message']);
                    }
                }
            } else {
                $this->line("! 没有银行明细数据，跳过详情测试");
            }

        } catch (\Exception $e) {
            $this->error("AdminBankDetailController测试失败: " . $e->getMessage());
        }
    }

    /**
     * 显示帮助信息
     */
    private function showHelp()
    {
        $this->info('使用示例:');
        $this->line('php artisan admin:test-controllers --test-recharge      # 测试充值控制器');
        $this->line('php artisan admin:test-controllers --test-bank-detail   # 测试银行明细控制器');
        $this->line('php artisan admin:test-controllers --test-all           # 测试所有控制器');
    }
}